package cn.stylefeng.guns.sys.modular.gslbDict.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.gslbDict.entity.Gslbzd;
import cn.stylefeng.guns.sys.modular.gslbDict.model.params.GslbzdParam;
import cn.stylefeng.guns.sys.modular.gslbDict.model.result.GslbzdResult;
import cn.stylefeng.guns.sys.modular.gslbDict.service.GslbzdService;
import cn.stylefeng.guns.sys.modular.importHelper.*;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;


/**
 * 公示类别字典控制器
 *
 * @author author
 * @Date 2021-01-11 11:19:58
 */
@Controller
@RequestMapping("/gslbzd")
public class GslbzdController extends BaseController {

    private String PREFIX = "/modular/gslbDict/gslbzd";

    private static boolean judegExcelEdition(String fileName) {
        if (fileName.matches("^.+\\.(?i)(xls)$")) {
            return false;
        } else {
            return true;
        }
    }

    @Autowired
    private GslbzdService gslbzdService;

    /**
     * 跳转到主页面
     *
     * @author author
     * @Date 2021-01-11
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/gslbzd.html";
    }

    /**
     * 新增页面
     *
     * @author author
     * @Date 2021-01-11
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/gslbzd_add.html";
    }

    /**
     * 编辑页面
     *
     * @author author
     * @Date 2021-01-11
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/gslbzd_edit.html";
    }

    /**
     * 新增接口
     *
     * @author author
     * @Date 2021-01-11
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(GslbzdParam gslbzdParam) {
        this.gslbzdService.add(gslbzdParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author author
     * @Date 2021-01-11
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(GslbzdParam gslbzdParam) {
        this.gslbzdService.updateGslbzdInfo(gslbzdParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author author
     * @Date 2021-01-11
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(GslbzdParam gslbzdParam) {
        this.gslbzdService.delete(gslbzdParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author author
     * @Date 2021-01-11
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(GslbzdParam gslbzdParam) {
        GslbzdParam param = new GslbzdParam();
        Gslbzd detail = new Gslbzd();
        param.setGsxxbh(gslbzdParam.getGsxxbh());
        GslbzdResult result = this.gslbzdService.findListBySpec(param).get(0);
        detail.setGsxxbh(result.getGsxxbh());
        detail.setGsxxlb(result.getGsxxlb());
        detail.setQx(result.getQx());
        detail.setSfsjtp(result.getSfsjtp());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author author
     * @Date 2021-01-11
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(GslbzdParam gslbzdParam) {
        return this.gslbzdService.findPageBySpec(gslbzdParam);
    }


    /**
     * 查询列表
     *
     * @author author
     * @Date 2021-01-11
     */
    @ResponseBody
    @RequestMapping("/listAll")
    public List<GslbzdResult> listAll(GslbzdParam gslbzdParam) {
        return this.gslbzdService.findListBySpec(gslbzdParam);
    }


    /**
     * 删除接口
     */
    @ResponseBody
    @RequestMapping("/allDelete")
    public Integer allDelete(@RequestParam String[] ids) {
        Integer returnNumber = 0;
        for (int i = 0; i < ids.length; i++) {
            this.gslbzdService.selectDeleteInfo(ids[i]);
            returnNumber++;
        }
        return returnNumber;
    }

    /**
     * 导入数据
     *
     * @param file
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/importData")
    public ResponseData importData(MultipartFile file) throws IOException {
        InputStream in = file.getInputStream();
        int index = 0;
        String fileName = file.getOriginalFilename();
        Workbook workbook = null;
        if (judegExcelEdition(fileName)) {
            workbook = new XSSFWorkbook(in);
        } else {
            workbook = new HSSFWorkbook(in);
        }
        //获取第一张工作表
        Sheet sheet = workbook.getSheetAt(0);
//        System.out.println(sheet.getLastRowNum());
//        System.out.println(sheet.getPhysicalNumberOfRows());
        String sfsjtp = "";
        for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
            //循环获取工作表的每一行
            if (i != 0) {
                Row sheetRow = sheet.getRow(i);
                //循环获取每一列
                GslbzdParam param = new GslbzdParam();
                param.setGsxxlb(sheetRow.getCell(0).getStringCellValue());
                param.setQx((int) sheetRow.getCell(1).getNumericCellValue());
                sfsjtp = sheetRow.getCell(2).getStringCellValue();
                if (sfsjtp.equals("是")) {
                    param.setSfsjtp("1");
                } else {
                    param.setSfsjtp("2");
                }
                this.gslbzdService.add(param);
                index++;
            }
        }
        //关闭资源
        workbook.close();
        if (index == sheet.getLastRowNum()) {
            return ResponseData.success(400, "导入成功", null);
        } else {
            return ResponseData.error("导入失败");
        }

    }

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    /**
     * 导入数据
     *
     * @param file
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/importData_new")
    public ResponseData importData_new(MultipartFile file) throws IOException {

        ExcelConfig excelConfig = new ExcelConfig(file, 0, 1, -1, 0, 2, "project-wxjj.GSLBZD");
        excelConfig.setBatchSize(100);

        IBaseExcel ii = new BaseExcelHandle() {

            @Override
            protected int checkRow(Object[] row, DataStore data, IContext context) {
                // TODO Auto-generated method stub
//				System.out.println("外部打印：" + Arrays.toString(row));
                Object[] temp = data.getData();
                temp[0] = UUID.randomUUID().toString();
                temp[1] = row[0];
                temp[2] = row[1];
                if (row[2].equals("是")) {
                    temp[3] = "1";
                } else {
                    temp[3] = "2";
                }
                data.setData(temp);
                return BaseExcelHandle.GO_CONTINUE;
            }

            @Override
            protected Object beforeHandle() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            protected Object afterHandle() {
                // TODO Auto-generated method stub
//				System.out.println("s2: " + System.currentTimeMillis());
                return null;
            }

        };

        ii.setSqlSessionTemplate(sqlSessionTemplate);
        ii.run(excelConfig);

        return ResponseData.success(400, "导入成功", null);

    }

}


